<?php

/*
	info: 模型基类
	$Id: modelbase.class.php 14 2011-08-29 08:48:19Z lfly1573@gmail.com $
*/

if (!defined('WEB_IN'))
{
	header("HTTP/1.0 404 Not Found");
	exit();
}

class ModelBase
{
	/**
	 * @var Database
	 */
	protected $db = NULL;

	/**
	 * @var Database
	 */
	protected $dbc = NULL;

	/**
	 * 构造函数
	 * @param $bolConnectDb 是否连接数据库
	 * @return void
	 */
	public function __construct($bolConnectDb = TRUE)
	{
		if ($bolConnectDb)
		{
			$this->ConnectDatabase();
		}
	}


	/****************************************************************/


	/**
	 * 连接系统配置数据库
	 * @return void
	 */
	protected final function ConnectDatabase()
	{
		if (!isset(Lfly::$webModel['Database']) || !is_object(Lfly::$webModel['Database']))
		{
			Lfly::$webModel['Database'] = new Database(Lfly::$webConfig['dbHost'], Lfly::$webConfig['dbUser'], Lfly::$webConfig['dbPass'], Lfly::$webConfig['dbPconnect'], Lfly::$webConfig['dbCharset'], Lfly::$webConfig['dbName'], Lfly::$webConfig['dbPre']);
			Lfly::$webServer['queryNum'] =& Lfly::$webModel['Database']->queryNum;
			Lfly::$webServer['queryInfo'] =& Lfly::$webModel['Database']->queryInfo;
		}
		if (!is_object($this->db))
		{
			$this->db =& Lfly::$webModel['Database'];
		}
	}

	/**
	 * 连接统计数据库
	 * @return void
	 */
	protected final function ConnectDatabaseForCount()
	{
		if (!isset(Lfly::$webModel['DatabaseForCount']) || !is_object(Lfly::$webModel['DatabaseForCount']))
		{
			if (!isset(Lfly::$webConfig['dbNameForCount']) || Lfly::$webConfig['dbNameForCount']=='' || Lfly::$webConfig['dbNameForCount']==Lfly::$webConfig['dbName'])
			{
				$this->ConnectDatabase();
				Lfly::$webModel['DatabaseForCount'] =& Lfly::$webModel['Database'];
				Lfly::$webServer['queryNumForCount'] = 0;
				Lfly::$webServer['queryInfoForCount'] = '';
			}
			else
			{
				Lfly::$webModel['DatabaseForCount'] = new Database(Lfly::$webConfig['dbHost'], Lfly::$webConfig['dbUser'], Lfly::$webConfig['dbPass'], Lfly::$webConfig['dbPconnect'], Lfly::$webConfig['dbCharset'], Lfly::$webConfig['dbNameForCount'], Lfly::$webConfig['dbPre']);
				Lfly::$webServer['queryNumForCount'] =& Lfly::$webModel['DatabaseForCount']->queryNum;
				Lfly::$webServer['queryInfoForCount'] =& Lfly::$webModel['DatabaseForCount']->queryInfo;
			}
		}
		if (!is_object($this->dbc))
		{
			$this->dbc =& Lfly::$webModel['DatabaseForCount'];
		}
	}

	/**
	 * 插入新数据
	 * @param $strTable 表名
	 * @param $aryDefault 字段默认值
	 * @param $aryData 设定的值地址传入
	 * @return bool 是否成功
	 */
	protected final function Insert($strTable, $aryDefault, &$aryData)
	{
		$tempFieldArray = $tempValueArray = array();
		foreach ($aryDefault as $tempKey => $tempValue)
		{
			$tempFieldArray[] = $tempKey;
			if (isset($aryData[$tempKey]))
			{
				$tempValueArray[] = $aryData[$tempKey];
			}
			else
			{
				$tempValueArray[] = $aryData[$tempKey] = $tempValue;
			}
		}
		if (empty($tempFieldArray))
		{
			return FALSE;
		}
		$tempResult = $this->db->Query("INSERT INTO {$strTable}(".implode(',', $tempFieldArray).") VALUES('".implode("','", $tempValueArray)."')");
		return $tempResult;
	}

	/**
	 * 更新数据
	 * @param $strTable 表名
	 * @param $aryData 设定的值
	 * @param $strCondition 执行条件
	 * @return bool 是否成功
	 */
	protected final function Update($strTable, $aryData, $strCondition = '')
	{
		$tempUpdateArray = array();
		foreach ($aryData as $tempKey => $tempValue)
		{
			$tempUpdateArray[] = "{$tempKey}='{$tempValue}'";
		}
		if (empty($tempUpdateArray))
		{
			return FALSE;
		}
		$tempWhereSql = ($strCondition != '') ? " WHERE {$strCondition}" : "";
		$tempResult = $this->db->Query("UPDATE {$strTable} SET ".implode(',', $tempUpdateArray).$tempWhereSql);
		return $tempResult;
	}

}	//end class

?>